package com.bwei.like.mostitemnews.adapater;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwei.like.mostitemnews.R;
import com.bwei.like.mostitemnews.bean.Bean;
import com.bwei.like.mostitemnews.utils.ImageLoaderUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by LiKe on 2016/11/23.
 */
public class MyAdapater extends BaseAdapter {

    private static final String TAG = "MyAdapater";
    private Context context;
    private Bean bean;
    private static final int PIC_NORMAL = 0;
    private static final int PIC_MOST = 1;

    public MyAdapater(Bean bean, Context context) {
        this.bean = bean;
        this.context = context;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (bean.getIdlist().get(0).getNewslist().get(position).getThumbnails_qqnews().size() == 1) {
            return PIC_NORMAL;
        } else {
            return PIC_MOST;
        }
    }


    @Override
    public int getCount() {
        return bean.getIdlist().get(0).getNewslist().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            switch (itemViewType) {
                case PIC_NORMAL:
                    convertView = View.inflate(context, R.layout.pic_normal_item, null);
                    viewHolder.titletv = (TextView) convertView.findViewById(R.id.title_tv);
                    viewHolder.normalpic = (ImageView) convertView.findViewById(R.id.normal_pic);
                    break;

                case PIC_MOST:
                    convertView = View.inflate(context, R.layout.pic_most_item, null);
                    viewHolder.titletv = (TextView) convertView.findViewById(R.id.title_tv);
                    viewHolder.pic1 = (ImageView) convertView.findViewById(R.id.pic1);
                    viewHolder.pic2 = (ImageView) convertView.findViewById(R.id.pic2);
                    viewHolder.pic3 = (ImageView) convertView.findViewById(R.id.pic3);
                    viewHolder.pic4 = (ImageView) convertView.findViewById(R.id.pic4);
                    break;
            }
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        switch (itemViewType) {
            case PIC_NORMAL:
                viewHolder.titletv.setText(bean.getIdlist().get(0).getNewslist().get(position).getTitle());
                ImageLoader.getInstance().displayImage(bean.getIdlist().get(0).getNewslist().get(position).getThumbnails_qqnews().get(0),viewHolder.normalpic, ImageLoaderUtils.initOptions());
                break;
            case PIC_MOST:
                String title = bean.getIdlist().get(0).getNewslist().get(position).getTitle();
                viewHolder.titletv.setText(title);
                ImageLoader.getInstance().displayImage(bean.getIdlist().get(0).getNewslist().get(position).getThumbnails_qqnews().get(0),viewHolder.pic1, ImageLoaderUtils.initOptions());
                ImageLoader.getInstance().displayImage(bean.getIdlist().get(0).getNewslist().get(position).getThumbnails_qqnews().get(1),viewHolder.pic2, ImageLoaderUtils.initOptions());
                ImageLoader.getInstance().displayImage(bean.getIdlist().get(0).getNewslist().get(position).getThumbnails_qqnews().get(2),viewHolder.pic3, ImageLoaderUtils.initOptions());
                ImageLoader.getInstance().displayImage(bean.getIdlist().get(0).getNewslist().get(position).getThumbnails_qqnews().get(3),viewHolder.pic4, ImageLoaderUtils.initOptions());
                break;
        }
        return convertView;
    }

    public class ViewHolder {
        public ImageView pic1, pic2, pic3,pic4;
        public ImageView normalpic;
        public TextView titletv;

    }
}
